package com.sky.service.impl;


import com.sky.constant.MessageConstant;
import com.sky.entity.Orders;
import com.sky.exception.OrderBusinessException;
import com.sky.mapper.OrdersMapper;
import com.sky.service.adminOrderService;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.OrderStatisticsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class adminOrderServiceImpl implements adminOrderService {
    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 完成订单
     *
     * @param id
     */
    @Override
    public void completeOrder(Long id) {
        //将id封装进Orders中
        //根据id查询订单
        Orders orders=ordersMapper.getById(id);
        // 校验订单是否存在，并且状态为4
        if (orders == null || !orders.getStatus().equals(Orders.DELIVERY_IN_PROGRESS)) {
            throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
        }
        Orders orders02=new Orders();
        orders02.setId(orders.getId());
        // 更新订单状态,状态转为完成
        orders02.setStatus(Orders.COMPLETED);
        orders02.setDeliveryTime(LocalDateTime.now());

        ordersMapper.update(orders02);

    }

    @Override
    public OrderStatisticsVO ordercount() {
        //将数据封装成一个OrderOverViewVO类型的对象
        //获得各个状态的订单数量
        Integer toBeConfirmed=ordersMapper.countStatus(Orders.TO_BE_CONFIRMED);
        Integer confirmed=ordersMapper.countStatus(Orders.CONFIRMED);
        Integer deliveryInProgress=ordersMapper.countStatus(Orders.DELIVERY_IN_PROGRESS);

        // 将查询出的数据封装到orderStatisticsVO中响应
        OrderStatisticsVO orderStatisticsVO=new OrderStatisticsVO();

        orderStatisticsVO.setToBeConfirmed(toBeConfirmed);
        orderStatisticsVO.setConfirmed(confirmed);
        orderStatisticsVO.setDeliveryInProgress(deliveryInProgress);

        return orderStatisticsVO;

    }
}
